//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


namespace Shp.Runtime.DataModel.Sitka.Tests
{
    using System;
    using System.Collections.Generic;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Shp.Runtime.Contract;

    [TestClass]
    public class AuthorizationDataModelFixture
    {
        private MetadataInitialize metadataInitializeData;
        private TestableRepositoryFactory factory;

        [TestInitialize]
        public void Initialize()
        {
            metadataInitializeData = new MetadataInitialize();

            factory = new TestableRepositoryFactory(metadataInitializeData.TenantId);
            factory.CreateContainers();

            metadataInitializeData.Container = factory.MetadataContainerName;
            metadataInitializeData.CreateActionsAndAuthorizations();
        }

        [TestCleanup]
        public void CleanUp()
        {
            factory.DeleteContainers();
        }

        [TestMethod]
        public void ShouldGetActions()
        {
            AuthorizationDataModel authorizationDataModel = new AuthorizationDataModel(factory);
            List<string> credentials = new List<string>();
            credentials.Add("credential1");
            credentials.Add("credential3");
            string[] actions = authorizationDataModel.GetActions("TestAuthorizationTenant2".ToLower(), credentials.ToArray());

            Assert.IsNotNull(actions);
            Assert.AreEqual(2, actions.Length);
            Assert.AreEqual(@"http://tempuri.org/IDatamodelSC/UpdateViewDef", actions[0]);
            Assert.AreEqual(@"http://tempuri.org/ISystemSC/Ping", actions[1]);            
        }

        [TestMethod]
        public void ShouldGetCredentialPermissionList()
        {
            AuthorizationDataModel authorizationDataModel = new AuthorizationDataModel(factory);
            CredentialPermission[] credentials = authorizationDataModel.GetCredentialPermissionList("TestAuthorizationTenant1", "credential1");
            Assert.IsNotNull(credentials);
            Assert.AreEqual(2, credentials.Length);
        }

        [TestMethod]
        public void ShouldUpdateCredentialPermission()
        {
            AuthorizationDataModel authorizationDataModel = new AuthorizationDataModel(factory);

            List<Guid> actions = new List<Guid>();
            actions.Add(metadataInitializeData.ActionId1);
            authorizationDataModel.UpdateCredentialPermission("TestAuthorizationTenant1", "credential1", actions.ToArray());

            CredentialPermission[] credentials = authorizationDataModel.GetCredentialPermissionList("TestAuthorizationTenant1", "credential1");
            Assert.IsNotNull(credentials);
            Assert.AreEqual(1, credentials.Length);
        }
    }

}
